import configInit from './extend/config'
import themeInit from './extend/theme'
import layoutInit from './extend/layout'
import notifyInit from './extend/notify'
import navigatorInit from './extend/navigator'
import requestInit from './extend/request'
import authInit from './extend/auth'
import apiInit from './extend/api'
import utilInit from './extend/util'

const $x = {
  xmMap() {
    let ret = {
      theme: uni.$x.theme,
    }
    if($x.xmMapExtend) {
      ret = $x.xmMapExtend(ret, uni.$x)
    }
    return ret
  },
}

let xoneMixin = {
  computed: {
    $xm() {
      return uni.$x.xmMap()
    }
  },
  onLoad() {
    uni.$x.mpShare = {
      title: '', // 默认为小程序名称
      path: '', // 默认为当前页面路径
      imageUrl: '' // 默认为当前页面的截图
    }
  },
  onShareAppMessage() {
    if($x.onShareAppMessageExtend) {
      return $x.onShareAppMessageExtend(uni.$x.mpShare, uni.$x)
    }else{
      return uni.$x.mpShare
    }
  }
}

export function install(Vue, options, extendlist=[]) {
  uni.$x = $x

  configInit($x, options, Vue)
  themeInit($x, options, Vue)
  layoutInit($x, options, Vue)
  notifyInit($x, options, Vue)
  navigatorInit($x, options, Vue)
  requestInit($x, options, Vue)
  authInit($x, options, Vue)
  apiInit($x, options, Vue)
  utilInit($x, options, Vue)

  extendlist.forEach(v=>v($x, options, Vue))

  $x.app = { ...options.appExtend }
  Object.assign($x, options.extend||{})

  // #ifndef APP-NVUE
  Vue.prototype.$x = $x
  Vue.mixin(xoneMixin)
  // #endif

  _addPlugin = plugin=>{
    plugin($x, options, Vue)
  }
}

let _addPlugin = null
export function addPlugin(plugin) {
  if(!plugin||!_addPlugin) return
  return _addPlugin(plugin)
}
export function removePlugin(plugin) {
  
}
export default {
  install
}